<?php
/**
 * Created by PhpStorm.
 * User: PC
 * Date: 2017/3/22
 * Time: 10:22
 */

namespace Admin\Controller;

use Admin\Controller\Tencentyun\ImageV2;
use Common\Controller\AdminbaseController;
use Common\Model\NewsModel;
use Think\Cache\Driver\Redis;
use Think\Model;

class SensitiveWordsController extends AdminbaseController
{
    private $admin_id;
    private $sensitive_words;

    public function __construct()
    {
        parent::__construct();
        $this->admin_id = session('ADMIN_ID');
        $this->sensitive_words = M("vrnews1.sensitive_words",null);
    }

    public function index(){
        $find_words = I("find_words");
        $currentPage = I('p', 1);
        if ($currentPage <= 0) {
            $currentPage = 1;
        }
        $pageSize = '30';
        $pageStart = ($currentPage - 1) * $pageSize;
        $where = " 1=1 ";
        if($find_words){
            $where .= " and words like '%".$find_words."%'";
        }
        $words_total = $this->sensitive_words->field('words')->select();
        $redis = new Redis();
        $redis->set(C("environment").'_sensitive_word', $words_total);
        $key = $redis->buildKey(C("environment").'_sensitive_words');
        $redis->set_redis($key, $words_total);
        $words_list = $this->sensitive_words->where($where)->limit($pageStart, $pageSize)->select();
        $pageCount  = $this->sensitive_words->where($where)->count();
        $page = $this->page($pageCount, $pageSize);
        $this->assign('words_list', $words_list);
        $this->assign('admin_id', $this->admin_id);
        $this->assign('find_words', $find_words);
        $this->assign('currentPage', $currentPage);
        $this->assign('page', $page->show('Admin'));
        $this->display();
    }
    
    public function addWords(){
        if(IS_POST && I('keyword') == 'add'){
            $count = 0;
            $have  = 0;
            $total_count = 0;
            $words = I("words");
            if(!$words && !$_FILES['words_file']['name']){
                $this->error('请添加敏感词');
            }
            if($_FILES['words_file']['name']){
                $filename = $_FILES['words_file']['name'];
                $allowExt = array("txt");
                $ext = pathinfo($filename,PATHINFO_EXTENSION);
                if(!in_array($ext,$allowExt)){
                    $this->error('只能上传.txt类型文件');
                }
                $path    = SITE_PATH.'/data/upload/';
                $uniName = time().".".$ext;
                $destination = $path.$uniName;
                $tmp_name = $_FILES['words_file']["tmp_name"];
                if(@move_uploaded_file($tmp_name,$destination)){
                    $content = file_get_contents($destination);
//                    var_dump($content);exit();
                    if(strstr($content, '、')){
                        $word_list = explode('、', $content);
                    }elseif (strstr($content, ',')){
                        $word_list = explode(',', $content);
                    }elseif (strstr($content, '，')){
                        $word_list = explode('，', $content);
                    }else{
                        $word_list = array($content);
                    }
                    if(count($word_list) > 0){
                        $total_count += count($word_list);
                        for ($i=0;$i<count($word_list);$i++){
                            $word = $this->sensitive_words->where("words = '".$word_list[$i]."'")->find();
                            if($word){
                                $have += 1;
                                continue;
                            }
                            $data['words'] = $word_list[$i];
                            $data['create_time'] = time();
                            $count += 1;
                            $dataList[] = $data;
                        }
                        $this->sensitive_words->addAll($dataList);
                        unset($data);
                        unset($dataList);
                    }
                    unlink($destination);
                }else{
                    $this->error('文件上传失败');
                }
            }
            if($words){
                if(strstr($words, '、')){
                    $word_list = explode('、', $words);
                }elseif (strstr($words, ',')){
                    $word_list = explode(',', $words);
                }elseif (strstr($words, '，')){
                    $word_list = explode('，', $words);
                }else{
                    $word_list = array($words);
                }
                if(count($word_list) > 0){
                    $total_count += count($word_list);
                    for ($i=0;$i<count($word_list);$i++){
                        $word = $this->sensitive_words->where("words = '".$word_list[$i]."'")->find();
                        if($word){
                            $have += 1;
                            continue;
                        }
                        $data['words'] = $word_list[$i];
                        $data['create_time'] = time();
                        $count += 1;
                        $dataList[] = $data;
                    }
                    $this->sensitive_words->addAll($dataList);
                    unset($data);
                    unset($dataList);
                }
            }
            echo "<script>alert('上传成功，文件中包含敏感词".$total_count."个，已存在".$have."个，本次成功更新".$count."个');</script>";
        }
        $this->success('发布成功！', U("Admin/SensitiveWords/index"));
    }


    public function delWords(){
        $words_checbox_ids = implode(',',I("words_checbox"));
        $where['id'] = array('IN',$words_checbox_ids);
        $result = $this->sensitive_words->where($where)->delete();
        echo $result;exit();
    }
    
}